package io.renren.modules.collect.service.impl;

import io.renren.common.utils.R;
import io.renren.modules.collect.entity.CollectinfosCustomEntity;
import io.renren.modules.projectinfo.dao.ProjectinfosDao;
import io.renren.modules.user.dao.UserinfosDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.renren.modules.collect.dao.CollectinfosDao;
import io.renren.modules.collect.entity.CollectinfosEntity;
import io.renren.modules.collect.service.CollectinfosService;



/**
 * @author 黄清
 */
@Service("collectinfosService")
public class CollectinfosServiceImpl implements CollectinfosService {
	@Autowired
	private CollectinfosDao collectinfosDao;

	@Autowired
	private UserinfosDao userinfosDao;

	@Autowired
	private ProjectinfosDao projectinfosDao;

	@Override
	public int deleteByProject(String[] targetid) {
		int i = collectinfosDao.deleteByProject(targetid);
		return i;
	}

	@Override
	public int deleteByUser(String[] userinfoid) {
		return collectinfosDao.deleteByUser(userinfoid);
	}

	@Override
	public int apiDelete(Map<String, Object> map) {
		return collectinfosDao.apiDelete(map);
	}

	@Override
	public int apiDeleteBatch(Map<String, Object> map) {
		return collectinfosDao.apiDeleteBatch(map);
	}

	@Override
	public int queryApiTotal(Map<String, Object> map) {
		return collectinfosDao.queryApiTotal(map);
	}

	@Override
	public List<CollectinfosCustomEntity> apiCollectList(Map<String, Object> map) {
		return collectinfosDao.apiCollectList(map);
	}

	@Override
	public CollectinfosEntity queryObject(String collectinfoid){
		return collectinfosDao.queryObject(collectinfoid);
	}

	@Override
	public CollectinfosCustomEntity queryCustomInfo(String collectinfoid) {
		return collectinfosDao.queryCustomInfo(collectinfoid);
	}

	@Override
	public List<CollectinfosEntity> queryList(Map<String, Object> map){
		return collectinfosDao.queryList(map);
	}

	@Override
	public List<CollectinfosCustomEntity> queryCustomList(Map<String, Object> map) {
		return collectinfosDao.queryCustomList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map){
		return collectinfosDao.queryTotal(map);
	}

    @Override
    public int queryTotalByUser(Map<String, Object> map) {
        return collectinfosDao.queryTotalByUser(map);
    }

    @Override
	public void save(CollectinfosEntity collectinfos){
		//生成uuid，并设置id
		//生成项目的uuid，并设置给id
		UUID uuid = UUID.randomUUID();
		String id = uuid.toString().replaceAll("-", "");
		collectinfos.setCollectinfoid(id);
		//设置创建时间
		collectinfos.setCreatetime(new Date());
		collectinfosDao.save(collectinfos);
	}
	
	@Override
	public void update(CollectinfosEntity collectinfos){
		collectinfosDao.update(collectinfos);
	}
	
	@Override
	public void delete(String collectinfoid){
		collectinfosDao.delete(collectinfoid);
	}
	
	@Override
	public void deleteBatch(String[] collectinfoids){
		collectinfosDao.deleteBatch(collectinfoids);
	}

	@Override
	public R isExistCollect(CollectinfosEntity collectinfosEntity) {
		//判断用户是否存在
		int userIdExist = userinfosDao.isUserIdExist(collectinfosEntity.getUserinfoid());
		if(userIdExist == 0){
			return R.error("用户不存在");
		}
		//判断用户状态是否被锁定
		int lockUser = userinfosDao.isLockUser(collectinfosEntity.getUserinfoid());
		if(lockUser == 1){
			return R.error("用户已被锁定，无法操作！");
		}
		//判断项目是否存在
		int projectIdExist = projectinfosDao.isProjectIdExist(collectinfosEntity.getTargetid());
		if(projectIdExist == 0){
			return R.error("项目不存在");
		}
		//用户是否已经收藏该项目
		int existCollect = collectinfosDao.isExistCollect(collectinfosEntity);
		if(existCollect != 0){
			return R.error("该项目已经收藏了");
		}
		return null;
	}

	@Override
	public boolean collectState(CollectinfosEntity collectinfosEntity) {
		int existCollect = collectinfosDao.isExistCollect(collectinfosEntity);
		if(existCollect == 0){
			return false;
		}
		return true;
	}

}
